package com.applcn.user.controller.merchant;

import com.applcn.core.base.Result;
import com.applcn.user.constant.AuthRangeEnum;
import com.applcn.user.dto.merchant.DeleteRoleByIdsReqDTO;
import com.applcn.user.dto.merchant.PageRoleReqDTO;
import com.applcn.user.dto.merchant.RoleDetailReqDTO;
import com.applcn.user.dto.merchant.SaveOrUpdateRoleReqDTO;
import com.applcn.user.service.RoleService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * com.applcn.user.controller.merchant
 * Description:
 *
 * @author: 大妖怪
 * @date: 2020-03-17 01:54
 */
@RestController
@RequestMapping("/api/user/role")
@RequiredArgsConstructor
public class RoleController {

    private final RoleService roleService;

    /**
     * 分页查询角色列表
     *
     * @param pageNum
     * @param pageSize
     * @param roleName
     * @param authRange
     * @param shopId
     * @return
     */
    @GetMapping("/pageRole")
    public Result pageRole(@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                           @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
                           @RequestParam(value = "roleName", required = false) String roleName,
                           @RequestParam("authRange") Integer authRange,
                           @RequestParam("shopId") Long shopId) {
        PageRoleReqDTO dto = new PageRoleReqDTO();
        dto.setPageNum(pageNum);
        dto.setPageSize(pageSize);
        dto.setRoleName(roleName);
        dto.setAuthRange(authRange);
        dto.setShopId(shopId);
        return roleService.pageRole(dto);
    }

    /**
     * 创建/修改角色
     *
     * @param dto
     * @return
     */
    @GetMapping("/saveOrUpdateRole")
    public Result saveOrUpdateRole(@RequestBody SaveOrUpdateRoleReqDTO dto) {
        return roleService.saveOrUpdateRole(dto);
    }

    /**
     * 批量删除角色
     *
     * @param dto
     * @return
     */
    @PostMapping("/deleteRoleByIds")
    public Result deleteRoleByIds(@RequestBody DeleteRoleByIdsReqDTO dto) {
        return roleService.deleteRoleByIds(dto);
    }

    /**
     * 查询角色详情
     *
     * @param id
     * @param shopId
     * @return
     */
    @GetMapping("/roleDetail")
    public Result roleDetail(@RequestParam("id") Long id,
                             @RequestParam("shopId") Long shopId) {
        RoleDetailReqDTO dto = RoleDetailReqDTO.builder()
                .id(id)
                .shopId(shopId)
                .build();
        return roleService.roleDetail(dto);
    }

    /**
     * 查询店铺角色列表
     *
     * @return
     */
    @GetMapping("/shopRoleList")
    public Result shopRoleList() {
        return roleService.shopRoleList(AuthRangeEnum.MERCHANT);
    }
}
